/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReferenceUI.java
 *
 * Created on 5 mars 2012, 19:01:33
 */
package app.views.references;

import app.App;
import app.models.Reference;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author Ghost
 */
public class ReferenceUI extends javax.swing.JPanel implements MouseListener {
    
    private boolean hovered = false;

    private Reference reference;
    
    /** Creates new form ReferenceUI */
    public ReferenceUI() {
        initComponents();
        addMouseListener(this);
        setReference(null);
    }
    
    public void setReference(Reference ref){
        this.reference = ref;
        if(ref == null){
            lblDes.setText("Une référence de folie");
            lblCategorie.setText("Catégorie de test");
            lblPrix.setText("123.45");
            image.setImage(App.createImage("resources/photo/no-image.gif"));
        } else {
            lblDes.setText(reference.getDesignation());
            lblCategorie.setText(reference.getCategorie().getLibelle());
            lblPrix.setText(String.valueOf(reference.getPxVenteTTC()));
            String fileName;
            try{
                fileName = reference.getPhoto().getFileName();
                if("".equals(fileName)){
                    fileName = "no-image.gif";
                }
            } catch(Exception e){
                fileName = "no-image.gif";
            }
            
            image.setImage(App.createPhoto(fileName));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        image = new org.jdesktop.swingx.JXImagePanel();
        lblDes = new javax.swing.JLabel();
        lblCategorie = new javax.swing.JLabel();
        lblPrix = new javax.swing.JLabel();

        image.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        image.setMinimumSize(new java.awt.Dimension(128, 128));
        image.setPreferredSize(new java.awt.Dimension(128, 128));

        javax.swing.GroupLayout imageLayout = new javax.swing.GroupLayout(image);
        image.setLayout(imageLayout);
        imageLayout.setHorizontalGroup(
            imageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 126, Short.MAX_VALUE)
        );
        imageLayout.setVerticalGroup(
            imageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 124, Short.MAX_VALUE)
        );

        lblDes.setText("jLabel1");

        lblCategorie.setText("jLabel1");

        lblPrix.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDes, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                    .addComponent(image, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                    .addComponent(lblCategorie, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                    .addComponent(lblPrix, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblDes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(image, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblCategorie)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblPrix)
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXImagePanel image;
    private javax.swing.JLabel lblCategorie;
    private javax.swing.JLabel lblDes;
    private javax.swing.JLabel lblPrix;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mouseClicked(MouseEvent e) {
        //TODO
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {
        hovered = true;
        repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        hovered = false;
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        if(hovered){
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.LIGHT_GRAY);
        }
        g.drawRect(0, 0, getWidth() - 2, getHeight() - 2);
        
    }
}
